<%@page import="com.labot.types.LoginType"%>
<%@ include file="/common/taglibs.jsp" %>

<head>
    <title><fmt:message key="userProfile.title"/></title>
    <meta name="menu" content="UserMenu"/>
</head>

<c:set var="delObject" scope="request"><fmt:message key="userList.user"/></c:set>
<script type="text/javascript">var msgDelConfirm =
   "<fmt:message key="delete.confirm"><fmt:param value="${delObject}"/></fmt:message>";
</script>

<div class="span2">
    <h2><fmt:message key="userProfile.heading"/></h2>
    <c:choose>
        <c:when test="${(param.from == 'list' || param.from == 'listldap')}">
            <p><fmt:message key="userProfile.admin.message"/></p>
        </c:when>
        <c:otherwise>
            <p><fmt:message key="userProfile.message"/></p>
        </c:otherwise>
    </c:choose>
</div>
<div class="span7">
    <s:form id="userform" name="userForm" action="saveUser" method="post" cssClass="well form-horizontal" autocomplete="off">
        <s:hidden key="user.id"/>
        
        <input type="hidden" name="from" value="${ param.from }"/>

        <c:if test="${cookieLogin == 'true'}">
            <s:hidden key="user.password"/>
            <s:hidden key="user.confirmPassword"/>
        </c:if>

        <input type="hidden" name="encryptPass" id="encryptPass" value="${ user.id == null ? 'true' : 'false' }"/>

		<s:textfield key="user.username" required="true" onblur="changePreview()"/>

		<fieldset class="control-group">
            <label for="loginType" class="control-label"><fmt:message key="userProfile.loginType"/></label>
            <div class="controls">
                <select id="loginType" name="user.loginType" onchange="loginTypeToggle()">
                    <c:forEach items="<%= LoginType.values() %>" var="login">
                   	 	<option value="${login}" ${fn:contains(user.loginType, login) ? 'selected' : ''}><fmt:message key="loginType.${login}"/></option>
                    </c:forEach>
                </select>
            </div>
        </fieldset>
        
       
        
	        <fieldset class="control-group">
		        <div id='photoPreviewLdap'>
		        	<label for="photoLdap" class="control-label"><fmt:message key="userProfile.ldapPreview"/></label>
		        	<div class="controls">
			        	<img id="photoLdap" src="${ ctx }/thumbnailPhotoLdap?username=${ user.username }"/>
		        	</div>
		        </div>
		        <div id='photoPreviewInternal'>
			        <label for="photoInternal" class="control-label"><fmt:message key="userProfile.gravatarPreview"/></label>
			        <div class="controls">
		        		<img id="photoInternal" src="${ ctx }/thumbnailPhoto?username=${ user.username }"/>
		        	</div>
		        </div>
	        </fieldset>
		<security:authorize ifAnyGranted="ROLE_ADMIN">
		</security:authorize>
        
        <div id='passwordFields'>
        <c:if test="${cookieLogin != 'true'}">
            <s:password name="user.password" showPassword="true" key="user.password" 
                        onchange="passwordChanged(this)"/>

            <s:password key="user.confirmPassword" 
                        showPassword="true" onchange="passwordChanged(this)"/>
        </c:if>
        </div>

        <s:textfield key="user.passwordHint" required="true"/>
        <s:textfield key="user.fullName" required="true"/>
        <s:textfield key="user.email" />
        <s:textfield key="user.phoneNumber" />
        
		<c:choose>
		    <c:when test="${(param.from == 'list' || param.from == 'listldap')}">
		        <fieldset class="control-group">
		            <label class="control-label"><fmt:message key="userProfile.accountSettings"/></label>
		            <div class="controls">
		                <label class="checkbox inline">
		                    <s:checkbox key="user.enabled" id="user.enabled" theme="simple" fieldValue="true"/>
		                    <fmt:message key="user.enabled"/>
		                </label>
		                <label class="checkbox inline">
		                    <s:checkbox key="user.accountExpired" id="user.accountExpired" theme="simple" fieldValue="true"/>
		                    <fmt:message key="user.accountExpired"/>
		                </label>
		                <label class="checkbox inline">
		                    <s:checkbox key="user.accountLocked" id="user.accountLocked" theme="simple" fieldValue="true"/>
		                    <fmt:message key="user.accountLocked"/>
		                </label>
		                <br/>
		                <label class="checkbox inline">
		                    <s:checkbox key="user.credentialsExpired" id="user.credentialsExpired" theme="simple" fieldValue="true"/>
		                    <fmt:message key="user.credentialsExpired"/>
		                </label>
		            </div>
		        </fieldset>
		        <fieldset class="control-group">
		            <label for="userRoles" class="control-label"><fmt:message key="userProfile.assignRoles"/></label>
		            <div class="controls">
		                <select id="userRoles" name="userRoles" multiple="true">
		                    <c:forEach items="${availableRoles}" var="role">
		                    <option value="${role.value}" ${fn:contains(user.roles, role.label) ? 'selected' : ''}>${role.label}</option>
		                    </c:forEach>
		                </select>
		            </div>
		        </fieldset>
		    </c:when>
		    <c:otherwise>
		        <fieldset class="control-group">
		            <label class="control-label"><fmt:message key="user.roles"/>:</label>
		            <div class="controls readonly">
		                <s:iterator value="user.roleList" status="status">
		                    <s:property value="label"/><s:if test="!#status.last">,</s:if>
		                    <input type="hidden" name="userRoles" value="<s:property value="value"/>"/>
		                </s:iterator>
		            </div>
		            <s:hidden name="user.enabled" value="%{user.enabled}"/>
		            <s:hidden name="user.accountExpired" value="%{user.accountExpired}"/>
		            <s:hidden name="user.accountLocked" value="%{user.accountLocked}"/>
		            <s:hidden name="user.credentialsExpired" value="%{user.credentialsExpired}"/>
		        </fieldset>
		    </c:otherwise>
		</c:choose>
        <div id="actions" class="form-actions">
            <s:submit type="button" cssClass="btn btn-primary" method="save" key="button.save" theme="simple">
                <i class="icon-ok icon-white"></i>
                <fmt:message key="button.save"/>
            </s:submit>
            <c:if test="${ (param.from == 'list' || param.from == 'listldap') and not empty user.id}">
                <s:submit type="button" cssClass="btn btn-danger" method="delete" key="button.delete"
                    onclick="return confirmMessage(msgDelConfirm)" theme="simple">
                    <i class="icon-trash"></i>
                    <fmt:message key="button.delete"/>
                </s:submit>
            </c:if>
            <div class="btn"  onclick="window.history.back();">
                <i class="icon-remove"></i>
                <fmt:message key="button.cancel"/>
            </div>
        </div>
    </s:form>
</div>

<c:set var="scripts" scope="request">
<script type="text/javascript">
	loginTypeToggle();

    function passwordChanged(passwordField) {
        if (passwordField.name == "user.password") {
            var origPassword = "<s:property value="user.password"/>";
        } else if (passwordField.name == "user.confirmPassword") {
            var origPassword = "<s:property value="user.confirmPassword"/>";
        }

        if (passwordField.value != origPassword) {
//             createFormElement("input", "hidden", "encryptPass", "encryptPass",
//                     "true", passwordField.form);
			$("#encryptPass").val('true');
        }
    }
    
    function loginTypeToggle() {
    	if($('#loginType').val() == 'INTERNAL') {
    		// Show password input
    		$('#passwordFields').show();
    		$('#photoPreviewInternal').show();
    		$('#photoPreviewLdap').hide();
    	} else {
    		// Hide password
    		$('#passwordFields').hide();
    		$('#photoPreviewInternal').hide();
    		$('#photoPreviewLdap').show();
    	}
    	changePreview();
    }
    
    function changePreview() {
    	if($('#loginType').val() == 'INTERNAL') {
    		$("#photoInternal").attr("src","${ ctx }/thumbnailPhoto?username=" + $("input[name$='user.username']").val());
    	} else {
    		$("#photoLdap").attr("src","${ ctx }/thumbnailPhotoLdap?username=" + $("input[name$='user.username']").val());
    	}
    }

</script>
</c:set>
<script type="text/javascript">
    $(document).ready(function() {
        $("input[type='text']:visible:enabled:first", document.forms['userForm']).focus();
    });
</script>
